<?php

/**
 * playerstat actions.
 *
 * @package    vogue
 * @subpackage playerstat
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class playerstatActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    { 
        $this->match = $match = Doctrine::getTable('BbMatch')->find($request->getParameter('matchId'));
        $this->team1 = $team1 = $match->getBbTeams();
        $this->team2 = $team2 = $match->getBbTeams_2();
        $this->players1 = $team1->getPlayers();
        $this->players2 = $team2->getPlayers();
        
        $stats = array();
        $rss = Doctrine::getTable('BbPlayerStat')->getStats($match->getId());
        foreach ($rss as $rs)
            $stats[$rs->getPId()] = $rs;
  
        $this->stats = $stats;
    }
    
    
    
    public function executeSave(sfWebRequest $request)
    { 
        $match = Doctrine::getTable('BbMatch')->find($request->getParameter('matchId'));
        $players1 = $match->getBbTeams()->getPlayers();
        $players2 = $match->getBbTeams_2()->getPlayers();
        $matchId = $match->getId();
        
        
        // save round scores
        $round = $request->getParameter('round', 1);
        $pts1 = $request->getParameter('total-pts');
        $pts2 = $request->getParameter('total-pts2');
        switch ($round)
        {
        	case 1:
        		$match->setRound1Score1($pts1);
            $match->setRound1Score2($pts2);
        		break;
          case 2:
        		$match->setRound2Score1($pts1 - $match->getRound1Score1());
            $match->setRound2Score2($pts2 - $match->getRound1Score2());
        		break;
          case 3:
        		$match->setRound3Score1($pts1 - $match->getRound1Score1() - $match->getRound2Score1());
            $match->setRound3Score2($pts2 - $match->getRound1Score2() - $match->getRound2Score2());
        		break;
        	case 4:
        		$match->setRound4Score1($pts1 - $match->getRound1Score1() - $match->getRound2Score1() - $match->getRound3Score1());
            $match->setRound4Score2($pts2 - $match->getRound1Score1() - $match->getRound2Score1() - $match->getRound3Score1());
        		break;
        }
        $fullScore1 = intval($match->getRound1Score1()) + intval($match->getRound2Score1()) + intval($match->getRound3Score1()) + intval($match->getRound4Score1());
        $fullScore2 = intval($match->getRound1Score2()) + intval($match->getRound2Score2()) + intval($match->getRound3Score2()) + intval($match->getRound4Score2());
        $match->setScore1($fullScore1);
        $match->setScore2($fullScore2);
        $match->save();
          
        // save player stats - team1
        foreach ($players1 as $player)
        {
            $playerId = $player->getId();
            $rs = Doctrine::getTable('BbPlayerStat')->fetchone($matchId, $playerId);
            if (!$rs) 
            {
              $rs = new BbPlayerStat();
              $rs->setMId(intval($matchId));
              $rs->setPId(intval($playerId));
              $rs->save();
            }
            $rs->setFgm(intval($request->getParameter('fgm'.$playerId)));
            $rs->setFga(intval($request->getParameter('fga'.$playerId)));
            $rs->setPt3m(intval($request->getParameter('pt3m'.$playerId)));
            $rs->setPt3a(intval($request->getParameter('pt3a'.$playerId)));
            $rs->setFtm(intval($request->getParameter('ftm'.$playerId)));
            $rs->setFta(intval($request->getParameter('fta'.$playerId)));
            $rs->setOffreb(intval($request->getParameter('off'.$playerId)));
            $rs->setReb(intval($request->getParameter('reb'.$playerId)));
            $rs->setAst(intval($request->getParameter('ast'.$playerId)));
            $rs->setTov(intval($request->getParameter('tov'.$playerId)));
            $rs->setStl(intval($request->getParameter('stl'.$playerId)));
            $rs->setBlk(intval($request->getParameter('blk'.$playerId)));
            $rs->setPf(intval($request->getParameter('pf'.$playerId)));
            $rs->setPts(intval($request->getParameter('pts'.$playerId)));
            $rs->save();
        }
        
        
        // save player stats - team2
        foreach ($players2 as $player)
        {
            $playerId = $player->getId();
            $rs = Doctrine::getTable('BbPlayerStat')->fetchone($matchId, $playerId);
            if (!$rs) 
            {
              $rs = new BbPlayerStat();
              $rs->setMId(intval($matchId));
              $rs->setPId(intval($playerId));
              $rs->save();
            }
            $rs->setFgm(intval($request->getParameter('fgm'.$playerId)));
            $rs->setFga(intval($request->getParameter('fga'.$playerId)));
            $rs->setPt3m(intval($request->getParameter('pt3m'.$playerId)));
            $rs->setPt3a(intval($request->getParameter('pt3a'.$playerId)));
            $rs->setFtm(intval($request->getParameter('ftm'.$playerId)));
            $rs->setFta(intval($request->getParameter('fta'.$playerId)));
            $rs->setOffreb(intval($request->getParameter('off'.$playerId)));
            $rs->setReb(intval($request->getParameter('reb'.$playerId)));
            $rs->setAst(intval($request->getParameter('ast'.$playerId)));
            $rs->setTov(intval($request->getParameter('tov'.$playerId)));
            $rs->setStl(intval($request->getParameter('stl'.$playerId)));
            $rs->setBlk(intval($request->getParameter('blk'.$playerId)));
            $rs->setPf(intval($request->getParameter('pf'.$playerId)));
            $rs->setPts(intval($request->getParameter('pts'.$playerId)));
            $rs->save();
        }
        
  
        $this->getUser()->setFlash('success', 'Амжилттай хадгалагдлаа.', true);
        //$round = ($round < 4) ? (intval($round) + 1) : 4;
        $this->redirect('playerstat/index?matchId='.$matchId.'&round='.$round);
    }
    
    
    
    public function executeImport(sfWebRequest $request)
    { 
        ProjectConfiguration::getApplicationConfiguration('admin', 'dev', true)->loadHelpers('Url');
     
        $matchId = $request->getParameter('matchId');
        $round = $request->getParameter('round');
        
        $upload_dir = sfConfig::get('sf_uploads_dir').'/stats/';
        
        $resultMsg = "";      
        if($request->isMethod('POST'))
        {
            if($_FILES['importfile']['tmp_name']) 
            {
              $importfilename = $upload_dir.basename($_FILES['importfile']['name']);
        
              // upload file
              move_uploaded_file($_FILES['importfile']['tmp_name'], $importfilename);
              
              // check uploaded files          
              if(!file_exists($importfilename)) $resultMsg = "Алдаа: Файл хуулахад алдаа гарлаа. Файлыг шалгаад дахин хуулна уу.\n";
    
            	if(!fopen($importfilename,"r"))  $resultMsg = "Алдаа: Файлыг нээхэд алдаа гарлаа.\n";
            	
            	if(!filesize($importfilename)) $resultMsg = "Алдаа: Файл хоосон байна.\n";
        
            	if($resultMsg == "") $resultMsg = importCSV($importfilename);  
            } else $resultMsg = "Импорт хийх файлыг сонгоно уу.";
            
            $this->getUser()->setFlash('info', $resultMsg, true);    
        }
        $this->redirect('playerstat/index?matchId='.$matchId.'&round='.$round);
    }
    
    public function executeExport(sfWebRequest $request)
    { 
        ProjectConfiguration::getApplicationConfiguration('admin', 'dev', true)->loadHelpers('Global');
        
        $matchId = $request->getParameter('matchId');
        $match = Doctrine_Core::getTable('BbMatch')->find($matchId);
        
        $values_query = "SELECT * FROM bb_player_stat WHERE m_id = ".$matchId;
        $col_names_query = "SHOW COLUMNS FROM bb_player_stat;";
        $filename = date('Ymd').'.'.myTools::stripText($match);
        exportCSV($values_query, $col_names_query, $filename);
  
        return sfView::NONE;
    }

}